package com.mvd.app.ac2021.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kongzue.dialog.v3.WaitDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.activity.BaseActivity;
import com.mvd.app.adapter.WaiKuoListAdapter;
import com.mvd.app.api.ApiConfig;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.CommonUtils;
import com.mvd.app.commons.JsonUtil;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.CarsInforModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @version V1.0
 * @Title: WaiKuoListActivity
 * @Package com.mvd.app.ac2021.activitys
 * @Description: 外廓车辆列表
 * @date 2021-01-08 15:20
 **/

public class WaiKuoListActivity extends BaseActivity implements
        AdapterView.OnItemClickListener, ApiConfig {
    public WaiKuoListActivity instances = null;
    @BindView(R.id.ac_waikuo_list_et_search2)
    EditText acWaikuoListEtSearch2;
    @BindView(R.id.ac_waikuo_list_btn_search2)
    Button acWaikuoListBtnSearch2;
    @BindView(R.id.ac_waikuo_list_bt_type2)
    Button acWaikuoListBtType2;
    @BindView(R.id.ac_waikuo_list_btn_chongxin_xiaxian)
    Button acWaikuoListBtnChongxinXiaxian;
    @BindView(R.id.ac_set_view_m_2_image)
    TextView acSetViewM2Image;
    @BindView(R.id.ac_waikuo_list_btn_buchuan_zhaopian)
    Button acWaikuoListBtnBuchuanZhaopian;
    @BindView(R.id.ac_waikuo_list_lay_ll_statement)
    LinearLayout acWaikuoListLayLlStatement;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.ac_waikuo_list_listview)
    ListView acWaikuoListListview;
    private String Login_name = "", login_IDNumber;//当前登录的检测员的名字
    //adapter的过滤后的数据列表
    private List<CarsInforModel> adapterCarsList = new ArrayList<CarsInforModel>();
    private List<CarsInforModel> carsInforModelList_2 = new ArrayList<CarsInforModel>();
    WaiKuoListAdapter waiKuoListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wai_kuo_list);
        ButterKnife.bind(this);
        instances = this;
        String logStrf = "\n" + "onCreate-----外廓车辆列表-----" + "\n";
        PDALogUtils.createLogFile(0, logStrf.getBytes());
        Login_name = getIntent().getExtras().getString("login_UserID", "");
        login_IDNumber = getIntent().getExtras().getString("login_IDNumber", "");
        UtilsLog.e("外廓车辆列表==Login_name" + Login_name + "===login_IDNumber==" + login_IDNumber);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        Intent intent = new Intent(DaiJianCarsActivity.this, CarDetailsActivity.class);
//        startActivity(intent);
        CarsInforModel carsInforModel = (CarsInforModel) parent.getAdapter().getItem(position);
        UtilsLog.e("carsInforModel=33=" + carsInforModel.toString());
        Intent intent2 = null;
        //检测模式 0:外检  1：路试 2：底盘动态
//        if (CHECK_MODE == 0) {
//            //外检
////            intent2 = new Intent(DaiJianCarsActivity.this, VehicleDetailsActivity.class);
//            intent2 = new Intent(DaiJianCarsActivity.this, CarActivity.class);
////            intent2.putExtra("check_mode",BaseApplication.JIANCE_MODE);
//        } else if (CHECK_MODE == 1) {
//            //路试
//            intent2 = new Intent(DaiJianCarsActivity.this, RoadTestActivity.class);
////            intent2.putExtra("check_mode",BaseApplication.JIANCE_MODE);
//        } else {
//            //底盘动态
//            intent2 = new Intent(DaiJianCarsActivity.this, DiPanDongTaiActivity.class);
////            intent2 = new Intent(DaiJianCarsActivity.this, VehicleDetailsActivity.class);
////            intent2.putExtra("check_mode",BaseApplication.JIANCE_MODE);
//        }
        intent2.putExtra("model_CarsInforModel", carsInforModel);
        intent2.putExtra("Login_name", Login_name);
        intent2.putExtra("login_IDNumber", login_IDNumber);
        startActivity(intent2);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        String logStrf = "\n" + "onDestroy-----外廓车辆列表-----" + "\n";
        PDALogUtils.createLogFile(0, logStrf.getBytes());
        instances = null;
    }

    @OnClick({R.id.ac_waikuo_list_btn_search2, R.id.ac_waikuo_list_bt_type2, R.id.ac_waikuo_list_btn_chongxin_xiaxian, R.id.ac_waikuo_list_btn_buchuan_zhaopian, R.id.fab})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_waikuo_list_btn_search2:
                break;
            case R.id.ac_waikuo_list_bt_type2:
                break;
            case R.id.ac_waikuo_list_btn_chongxin_xiaxian:
                break;
            case R.id.ac_waikuo_list_btn_buchuan_zhaopian:
                break;
            case R.id.fab:
                break;
        }
    }


    private void getData(int type) {
        if (!TextUtils.isEmpty(SharedPreferencesUtils.getIP(this))) {
            if (CommonUtils.isOnline(this)) {
                if (type == 0) {
                    getDataCarsListByMode_2();
                } else if (type == 1) {

                }
            } else {
                Toast.makeText(this, getResources().getString(R.string.network_error), Toast.LENGTH_LONG)
                        .show();
            }
        } else {
            Toast.makeText(this, "请设置IP与端口号", Toast.LENGTH_LONG).show();
        }
    }

    //外廓车辆列表
    private void getDataCarsListByMode_2() {
        //检测模式 0:外检  F1 1：路试 R1 2：底盘动态 DC
        String car_url = "";
        switch (BaseApplication.JIANCE_MODE) {
            case 0:
                car_url = CAR_LIST_WAIJIAN;
                break;
            case 1:
                car_url = CAR_LIST_LUSHI;
                break;
            case 2:
                car_url = CAR_LIST_DIPAN;
                break;
        }
        if(adapterCarsList!=null){
            adapterCarsList=new ArrayList<>();
        }
        String logStr = "\n" + "外廓车辆列表------请求" + "\n" +
                "URL::" + SharedPreferencesUtils.getIP(instances) + car_url
                + "\n"  ;
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
        UtilsLog.e("getDataCarsListByMode-22--url==" + SharedPreferencesUtils.getIP(this) + car_url);
        OkGo.<String>get(SharedPreferencesUtils.getIP(this) + car_url).tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        WaitDialog.show(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
//                        UtilsLog.e("getDataCarsList-result==" + result);
                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        UtilsLog.e("外廓车辆列表-newResult=22=" + newResult);
                        String logStr = "\n" + "外廓车辆列表------onSuccess---" + "\n" +
                                "result::" + result+ "\n"  ;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                        if (!TextUtils.isEmpty(newResult) && !"[[]]".equals(newResult) && !"[{}]".equals
                                (newResult) && !"[]".equals(newResult)) {
                            carsInforModelList_2 = new ArrayList<CarsInforModel>();
                            carsInforModelList_2 = JsonUtil.stringToArray(newResult, CarsInforModel[].class);
                            if (carsInforModelList_2 != null && carsInforModelList_2.size() > 0) {
                                for (int i = 0; i < carsInforModelList_2.size(); i++) {
                                    adapterCarsList.add(carsInforModelList_2.get(i));
                                }
                            } else {
                                acWaikuoListListview.setVisibility(View.GONE);
                                UtilsLog.e("getDataCarsListByMode-result=22=" + "2222没有数据");
                            }
                        } else {
                            acWaikuoListListview.setVisibility(View.GONE);
//                            carsInforListAdapter.notifyDataSetChanged();
                            UtilsLog.e("getDataCarsListByMode-result=22=" + "没有数据");
                        }
                        acWaikuoListListview.setVisibility(View.VISIBLE);
                        acWaikuoListListview.setOnItemClickListener(WaiKuoListActivity.this);
//                        if (adapterCarsList != null && adapterCarsList.size() > 0) {
//                            carsInforListAdapter = new CarsInforListAdapter(WaiKuoListActivity.this,
//                                    adapterCarsList, CURRENT_CHECK_MODE);
//                            acWaikuoListListview.setAdapter(carsInforListAdapter);
//                            carsInforListAdapter.notifyDataSetChanged();
//                        } else {
//                            carsInforListAdapter = new CarsInforListAdapter(WaiKuoListActivity.this,
//                                    adapterCarsList, CURRENT_CHECK_MODE);
//                            acWaikuoListListview.setAdapter(carsInforListAdapter);
//                            carsInforListAdapter.notifyDataSetChanged();
//                        }
                        WaitDialog.dismiss();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        WaitDialog.dismiss();
                        UtilsLog.e("外廓车辆列表-onError=22=" + response.body());
                        String logStr = "\n" + "外廓车辆列表-----onError---" + "\n" +
                                "result::" + response.body()+ "\n"  ;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
//                        CommonUtils.hideLoadingDialog(instances);
                    }
                });
    }
}
